﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace SisLegal.Model
{

    public class FileLoad
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Guid { get; set; }
        public string Ext { get; set; }
        public int Type { get; set; }
        public int IDRef { get; set; }

        public FileLoad()
        {
            this.ID = 0;
            this.Name = "";
            this.Guid = "";
            this.Ext = "";
            this.IDRef = 0;
            this.Type = 0;
            //1 = principal, 2 = secundarios --> Expedient
            //11 = principal, 22 = secundarios --> FiscalFolder
        }
    }
    public class FileLoadManager
    {
        public List<FileLoad> GetAll()
        {
            List<FileLoad> result = new List<FileLoad>();
            string sqlQuery = String.Format("SELECT * FROM FileLoad ORDER BY ID");
            SqlConnection connection = new SqlConnection(Util.ConnKLegal);
            connection.Open();
            SqlCommand command = new SqlCommand(sqlQuery, connection);
            SqlDataReader dataReader = command.ExecuteReader();
            FileLoad process = null;
            if (dataReader.HasRows)
            {
                while (dataReader.Read())
                {
                    process = new FileLoad();
                    process.ID = Convert.ToInt32(dataReader["ID"]);
                    process.Name = dataReader["Name"].ToString();
                    result.Add(process);
                }
            }
            return result;
        }

        public List<FileLoad> GetAllFile(int ID, int type)
        {
            List<FileLoad> result = new List<FileLoad>();
            string sqlQuery = String.Format("SELECT * FROM FileLoad WHERE Type = {0} AND  IDRef = {1} ORDER BY ID", type, ID);
            SqlConnection connection = new SqlConnection(Util.ConnKLegal);
            connection.Open();
            SqlCommand command = new SqlCommand(sqlQuery, connection);
            SqlDataReader dataReader = command.ExecuteReader();
            FileLoad process = null;
            if (dataReader.HasRows)
            {
                while (dataReader.Read())
                {
                    process = new FileLoad();
                    process.ID = Convert.ToInt32(dataReader["ID"]);
                    process.Name = dataReader["Name"].ToString();
                    process.Guid = dataReader["Guid"].ToString();
                    process.Ext = dataReader["Ext"].ToString();
                    result.Add(process);
                }
            }
            return result;
        }

        public List<FileLoad> GetByIdRef(int articleId)
        {
            List<FileLoad> result = new List<FileLoad>();
            string sqlQuery = String.Format("select * from FileLoad where IDref={0}", articleId);
            SqlConnection connection = new SqlConnection(Util.ConnKLegal);
            connection.Open();
            SqlCommand command = new SqlCommand(sqlQuery, connection);
            SqlDataReader dataReader = command.ExecuteReader();
            FileLoad process = null;
            if (dataReader.HasRows)
            {
                while (dataReader.Read())
                {
                    process = new FileLoad();
                    process.ID = Convert.ToInt32(dataReader["ID"]);
                    process.Name = dataReader["Name"].ToString();
                    process.Guid = dataReader["Guid"].ToString();
                    process.Ext = dataReader["Ext"].ToString();
                    process.Type = Convert.ToInt32(dataReader["Type"]);
                    process.IDRef = Convert.ToInt32(dataReader["IDRef"]);
                    result.Add(process);
                }
            }

            return result;
        }

        public FileLoad GetById(int articleId)
        {
            FileLoad result = new FileLoad();
            string sqlQuery = String.Format("select * from FileLoad where ID={0}", articleId);
            SqlConnection connection = new SqlConnection(Util.ConnKLegal);
            connection.Open();
            SqlCommand command = new SqlCommand(sqlQuery, connection);
            SqlDataReader dataReader = command.ExecuteReader();

            if (dataReader.HasRows)
            {
                while (dataReader.Read())
                {
                    result.ID = Convert.ToInt32(dataReader["ID"]);
                    result.Name = dataReader["Name"].ToString();
                    result.Guid = dataReader["Guid"].ToString();
                    result.Ext = dataReader["Ext"].ToString();
                    result.Type = Convert.ToInt32(dataReader["Type"]);
                    result.IDRef = Convert.ToInt32(dataReader["IDRef"]);
                }
            }

            return result;
        }

        public Int32 Count()
        {
            List<FileLoad> result = new List<FileLoad>();
            string sqlQuery = String.Format("  SELECT COUNT(ID) AS total FROM FileLoad");
            SqlConnection connection = new SqlConnection(Util.ConnKLegal);
            connection.Open();
            SqlCommand command = new SqlCommand(sqlQuery, connection);
            command.CommandType = CommandType.Text;
            Int32 countStart = (Int32)command.ExecuteScalar();
            return countStart;
        }

        public List<FileLoad> Paginate(Int32 pag)
        {
            List<FileLoad> result = new List<FileLoad>();
            string sqlQuery = String.Format("SELECT * FROM (" +
                "SELECT ROW_NUMBER() OVER(ORDER BY ID) AS NUMBER, ID, Name FROM FileLoad " +
                ")as tbl " +
                "WHERE NUMBER BETWEEN((" + Convert.ToString(pag) + "-1) * (10 +1)) AND (" + Convert.ToString(pag) + " * 10) ORDER BY ID");

            SqlConnection connection = new SqlConnection(Util.ConnKLegal);
            connection.Open();
            SqlCommand command = new SqlCommand(sqlQuery, connection);
            SqlDataReader dataReader = command.ExecuteReader();
            FileLoad process = null;
            if (dataReader.HasRows)
            {
                while (dataReader.Read())
                {
                    process = new FileLoad();
                    process.ID = Convert.ToInt32(dataReader["ID"]);
                    process.Name = dataReader["Name"].ToString();
                    result.Add(process);
                }
            }
            return result;
        }

        public int Save(FileLoad obj)
        {
            string createQuery = String.Format("Insert into FileLoad (Name, Guid, Ext, Type, IDRef) Values('{0}','{1}','{2}','{3}','{4}');"
                + "Select @@Identity", obj.Name, obj.Guid, obj.Ext, obj.Type, obj.IDRef);

            SqlConnection connection = new SqlConnection(Util.ConnKLegal);
            connection.Open();
            SqlCommand command = null;

            command = new SqlCommand(createQuery, connection);

            int saveID = 0;
            try
            {
                var commandResult = command.ExecuteScalar();
                if (commandResult != null)
                {
                    saveID = Convert.ToInt32(commandResult);
                }
            }
            catch (Exception ex)
            {

            }

            command.Dispose();
            connection.Close();
            connection.Dispose();

            return saveID;
        }

        public int Update(FileLoad obj)
        {
            string updateQuery = String.Format("Update FileLoad SET Name='{0}' Where ID = {1};",
               obj.Name, obj.ID);

            SqlConnection connection = new SqlConnection(Util.ConnKLegal);
            connection.Open();
            SqlCommand command = null;
            command = new SqlCommand(updateQuery, connection);

            int saveID = 0;
            try
            {
                var commandResult = command.ExecuteScalar();
                if (commandResult != null)
                {
                    saveID = Convert.ToInt32(commandResult);
                }
                else
                {
                    saveID = obj.ID;
                }
            }
            catch (Exception ex)
            {

            }
            command.Dispose();
            connection.Close();
            connection.Dispose();

            return saveID;
        }

       

        public bool Delete(int articleId)
        {
            bool result = false;
            string sqlQuery = String.Format("delete from FileLoad where ID = {0}", articleId);

            SqlConnection connection = new SqlConnection(Util.ConnKLegal);
            connection.Open();
            SqlCommand command = new SqlCommand(sqlQuery, connection);
            int rowsDeletedCount = command.ExecuteNonQuery();
            if (rowsDeletedCount != 0)
                result = true;
            command.Dispose();
            connection.Close();
            connection.Dispose();
            return result;
        }

       
    }

}